<?php
class NewsCommentController  extends Zone_Action{	
	protected function fields( $default=NULL ){
		$data = array(
			content => array(
				type 		=> 'TEXT',
				no_empty 	=> true,
				min_length	=> 3,
				form_label 	=> 'Nội dung'
			),
			news_id => array(
				type 	   => 'INT',
				form_label => 'Công việc'
			),			
			date_updated	=>array(
				type	=> 'NONE',
				default_value => mysql_date()
			)
		);
		return $data;	
	}

	protected function notice( $title, $url, $msg ){
		$users = $this->Model->fetchAll("SELECT `ID` FROM `users` WHERE `is_deleted`='no'");		
		foreach( $users as $u ){	
			$this->Model->insert('notices',array(
				created_by_id	=> get_user_id(),
				user_id			=> $u['ID'],
				title			=> $title,
				url				=> $url,
				content			=> $msg,
				date			=> new Model_Expr('NOW()')
			));					
		}	
	}	
	
	
	public function initAction(){
		//if(!$this->hasRole('news')){
			//$this->setJSON(array(
			//	alert	=> error('Bạn không được phép truy cập phân hệ này')
			//));	
			//$this->stop();
		//}		
	}	
			
	public function indexAction(){}	
	
	public function addAction(){
		$this->removeLayout();
		$user_id = get_user_id();		
		$news_id = getInt('news_id',0);	
		$this->set('news_id', $news_id);		
				
		$post = $this->Model->fetchRow("SELECT * FROM `news` 
			WHERE `ID`='$news_id' AND `is_draft`='no' LIMIT 1");		

		if( !$post ){
			return $this->setJSON(array(
				alert => 'Bài viết đã bị xóa hoặc không tồn tại.'
			));
		}
				
		$comment = $this->Model->fetchRow("SELECT * FROM `news_comments` 
			WHERE `created_by_id`='$user_id' AND `is_draft`='yes' LIMIT 1");
		
		if( isset($_REQUEST['comment_id'])){
			$a = $this->Model->fetchRow("SELECT `a`.*,`b`.`username` as `created_by_name`
				FROM `news_comments` as `a` 
				LEFT JOIN `users` as `b` ON `a`.`created_by_id`=`b`.`ID`
				WHERE `a`.`ID`='".get('comment_id')."'");
			if($a){
				$this->set('quote',"<div class='quote'><i>Trích dẫn từ <b>{$a['created_by_name']}</b>:</i><br/>{$a['content']}</div>");
			}	
		}
		
		if( $comment ){
			$comment_id = $comment['ID'];
			$files = $this->Model->fetchAll("SELECT * 
				FROM `news_comments_files` WHERE `comment_id`='$comment_id'");	
		}else{
			$comment = array(
				is_draft		=> 'yes',
				created_by_id	=> $user_id
			);						
			$this->Model->insert("news_comments", $comment);
			$comment_id = $this->Model->lastId();
			$comment['ID'] = $comment_id;	
			$files = array();	
		}
		
		$this->set(array(			
			post	=> $comment,
			files 	=> $files	
		)); 
		
		if(isPost()){
			loadClass('ZData');
			$form = new ZData();
			$form->addFields( $this->fields());
			$data = $form->getData();
		
			if( !is_array( $data )){
				return $this->setJSON(array(
					message	=> error($data)
				));
			}				

			$data['is_draft'] = 'no';		
			$data['date_created'] = mysql_date();		
			$data['created_by_id'] = get_user_id();		
			
			$this->Model->update("news_comments",$data,"`ID`='$comment_id'");
			
			$this->notice($post['title'],'#News/Index/View?ID='.$news_id,'Thảo luận tin tức mới');
			
			$this->Model->update('news',array(
				total_comment	=> new Model_Expr('total_comment + 1')
			),"`ID`='$news_id'");			
		
			return $this->setJSON(array(
				redirect => "#News/Index/View?ID=$news_id"
			));				
		}		
	}
	
	public function editAction(){
		$user_id = get_user_id();		
		$comment_id = get('ID',0);
		$this->removeLayout();
		$comment = $this->Model->fetchRow("SELECT * FROM `news_comments`
			WHERE `is_draft`='no' AND `ID`='$comment_id' LIMIT 1");
		
		if( $comment && $comment['created_by_id'] != $user_id ){
			return $this->setJSON(array(
				alert	=> 'Bạn không có quyền sửa thảo luận này.'
			));
		}
		
		if( !$comment){
			return $this->setJSON(array(
				alert	=> 'Phản hồi đã bị xóa hoặc không tồn tại.'
			));
		}

		$news_id = $comment['news_id'];
		$post = $this->Model->fetchRow("SELECT * FROM `news` WHERE `ID`='$news_id'");				
			
		if( !$post ){
			return $this->setJSON(array(
				alert => 'Bài viết đã bị xóa hoặc không tồn tại'
			));
		}
		
		$this->set('post',$comment);
		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields());
			$form->removeField('news_id');
			
			$data = $form->getData();
						
			if( !is_array( $data )){
				return $this->setJSON(array(
					message	=> error($data)
				));
			}	

			$data['is_draft'] = 'no';		
			$this->Model->update("news_comments",$data,"`ID`='$comment_id'");
			$this->notice($post['title'],'#News/Index/View?ID='.$news_id,'Thảo luận tin tức mới');		

			return $this->setJSON(array(
				redirect => "#News/Index/View?ID=$news_id"
			));				
		}		
	}	

	public function deleteAction(){
		$user_id	= get_user_id();	
		$comment_id = get('ID',0);	
		$comment = $this->Model->fetchRow("SELECT * FROM `news_comments` 
			WHERE `ID`='$comment_id' AND `is_draft`='no'");
		if( !$comment ){
			return $this->setJSON(array(
				alert => 'Thảo luận không tồn tại'
			));
		}		
		
		if( $comment['created_by_id'] != $user_id ){
			return $this->setJSON(array(
				alert => 'Bạn không có quyền này.'
			));	
		}
		/*
		if( strtotime("+1 day",$comment['date_created']) > time() ){
			return $this->setJSON(array(
				alert => 'Thảo luận chỉ được chỉnh sửa trong vòng 24h.'
			));	
		}*/
		
		$this->Model->delete('news_comments',"`ID`='$comment_id'");
		$this->Model->update('news',array(
			total_comment	=> new Model_Expr('total_comment - 1')
		),"`ID`='{$comment['news_id']}'");

		
		$this->setJSON(array(
			redirect => "#News/Index/View?ID={$comment['news_id']}"
		));	
	}
}	